<template>
  <div>
    <div
      v-loading="loading"
    >
      <el-form ref="form" :model="form" label-width="120px" class="dataForm" :rules="formRules">
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('年份')" prop="year">
              <DatePicker v-model="form.year" type="year" value-format="yyyy" :placeholder="$t('年份')" @change="validatePcwData" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('报告周期')" prop="type">
              <el-select v-model="form.type" :placeholder="$t('报告周期')" @change="validatePcwData">
                <el-option
                  v-for="item in typeOptions"
                  :key="item.dictValue"
                  :value="item.dictValue"
                  :label="$t(item.dictLabel)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('币种')" prop="currency">
              <dictSelect
                v-model="form.currency"
                size="small"
                :clearable="true"
                dictionary="currency"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('数据来源')" prop="dataSource">
              <el-select v-model="form.dataSource" :placeholder="$t('数据来源')" @change="validatePcwData">
                <el-option
                  v-for="dict in dataSourceOptions"
                  :key="dict.dictValue"
                  :label="$t(dict.dictLabel)"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <i v-show="tipShow" class="el-icon-s-opportunity" style="color:#f39c12;font-size:16px" />
            <span v-show="tipShow" style="color:#f39c12">录入重复的年份，报告周期和数据来源时会覆盖之前的财务数据</span>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: right;margin-top: 20px;">
        <!--保存，保存并启动流程，取消 三个按钮-->
        <SaveBtn />
      </div>
    </div>

  </div>
</template>
<script>
import { getPcwDataMainData, validatePcwData, saveDataMain } from '@/api/company/pcw/pcwDataMain'
export default {
  props: {
    companyId: {
      type: String,
      default: undefined
    }
  },
     data() {
        return {
          form: {
              year: '',
              type: undefined,
              dataSource: 'act',
              currency: 'CNY'
          },
          loading: true,
          tipShow: false,
          rowId: undefined,
          dataSourceOptions: [],
          typeOptions: [],
          formRules: {
            year: [
              { required: true, message: this.$t('必填信息'), trigger: 'blur' }
            ],
            type: [
              { required: true, message: this.$t('必填信息'), trigger: 'blur' }
            ],
            dataSource: [
              { required: true, message: this.$t('必填信息'), trigger: 'blur' }
            ],
            currency: [
              { required: true, message: this.$t('必填信息'), trigger: 'blur' }
            ]
          }
      }
    },
    created() {
        // 财务数据报告周期
      this.getDicts('financial_type').then(response => {
        this.typeOptions = response.data
      })
      this.getDicts('pcw_data_source').then(response => {
        this.dataSourceOptions = response.data
      })
    },
    methods: {
      init(data) {
           this.loading = true
            if (data != null && data.id !== undefined) {
                this.rowId = data.id
                getPcwDataMainData(data.id).then(response => {
                  this.form = response.data
                  this.loading = false
             })
          }
      },
        // 验证同一公司下面，不能出现同一年度+同一报告周期的财务报表
    validatePcwData() {
      if (this.form.year && this.form.year !== '' && this.form.type && this.form.type !== '' && this.form.dataSource) {
        this.$nextTick(() => {
          validatePcwData({ year: this.form.year, type: this.form.type, companyId: this.companyId, dataSource: this.form.dataSource, id: this.rowId }).then(res => {
            if (res.data === null) {
                this.tipShow = true
            } else {
                this.tipShow = false
            }
          }).catch(() => {
            this.$set(this.form, 'type', undefined)
          })
        })
       }
      },
      submitForm: function() {
        this.$refs['form'].validate(valid => {
        if (valid) {
              this.loading = true
            saveDataMain(this.form).then(response => {
              if (response.code === 200) {
                  this.$message.success('保存成功')
                   this.$parent.$parent.updateformOpen = false
                  // 刷新list页面
                  this.$parent.$parent.getList()
              } else {
                 this.msgError(response.msg)
              }
              this.loading = false
            }).catch(() => {
                this.loading = false
                this.$message.error('保存失败')
            })
        }
        })
      },
      // 取消按钮
      cancel() {
        this.$parent.$parent.updateformOpen = false
        this.$parent.$parent.reset()
      }
    }
}
</script>

<style>

</style>
